#coding = utf-8  
import urllib, urllib2
import base64
from HTMLParser import HTMLParser
#<div class="m clearfix">

rhea_tag_div = 'div'
rhea_tag_name = 'class'
rhea_tag_value = 'm clearfix'


list_str = ('class','pagin fr')
rhea_tag_href = 'href'
aList = [ ]    

rhea_list_phone_tag = 'ul'
rhea_list_phone_class = ('class','list-h')

rhea_360_phone_img_class = ('class','p-img')
rhea_360_phone_name_class = ('class','p-name')
rhea_360_phone_price_class = ('class','p-price')




class Rhea_360buy_Brand_Parser(HTMLParser):
    
    brand_start_tag = 'dl'
    brand_start_tagid = ('id','select-brand')
    brand_start_div = 'div'
    brand_start_divid = 'rel'
    
    brand_start_tag_find = False
    brand_start_div_find = False
    
    brand_id = ''
    brand_name =''
    brand_url = ''
    
    def handle_starttag(self, tag, attrs):
        if tag == self.brand_start_tag and self.brand_start_tagid in attrs:
            self.brand_start_tag_find = True
            print "--start brand block--",tag
        if self.brand_start_tag_find:    
            #print "Encountered a start tag:", tag, attrs
            if tag == self.brand_start_div and  self.brand_start_divid in attrs[0]:
                self.brand_start_div_find = True
            
            if self.brand_start_div_find and tag == 'a':
                #print "find a brand:", tag, attrs
                self.brand_id = attrs[0][1]
                self.brand_url = attrs[1][1]     
                
           
    def handle_endtag(self, tag):
        if self.brand_start_tag_find and tag == self.brand_start_tag:
            self.brand_start_tag_find = False
            print "--end brand block--",tag      
        if self.brand_start_div_find  and tag == self.brand_start_div:
            self.brand_start_div_find = False
            #print "brand div end tag:", tag
            print "----------------"
            print "brand:",self.brand_id,self.brand_name.decode('gb2312'),self.brand_url   
            
    
    def handle_data(self, data):
        if self.brand_start_tag_find and self.brand_start_div_find:
            self.brand_name = data
            #print "find brand name :", data.decode('gb2312')        


class Rhea_360buy_Parser(HTMLParser):
    
    rhea_tag_find = False
    rhea_tag_phone_find = False
    rhea_tag_img_find = False
    rhea_tag_name_find = False
    rhea_tag_price_find = False
    
    rhea_360_phone_id =''
    rhea_360_phone_img =''
    rhea_360_phone_name=''
    rhea_360_phone_url=''
    rhea_360_phone_price=''
    rhea_360_phone_hot=''
    
    def handle_starttag(self, tag, attrs):
        if rhea_list_phone_tag == tag and rhea_list_phone_class in attrs:
            Rhea_360buy_Parser.rhea_tag_find = True
            #print "Encountered a start tag:", tag, attrs
            
        if  Rhea_360buy_Parser.rhea_tag_find:
            #print "Encountered a start tag1:", tag, attrs  
            
            if 'li' == tag and 'sku' in attrs[0][0]:
                Rhea_360buy_Parser.rhea_tag_phone_find = True
                Rhea_360buy_Parser.rhea_tag_img_find = False
                Rhea_360buy_Parser.rhea_tag_name_find  = False
                Rhea_360buy_Parser.rhea_tag_price_find  = False
                Rhea_360buy_Parser.rhea_360_phone_id = attrs[0][1]

            if tag == rhea_tag_div and rhea_360_phone_img_class in attrs:
                #print "find img tag"
                Rhea_360buy_Parser.rhea_tag_img_find  = True

                
            
            #img
            if  Rhea_360buy_Parser.rhea_tag_img_find:
                if tag == 'a' and rhea_tag_href in attrs[1]:
                    Rhea_360buy_Parser.rhea_360_phone_url = attrs[1][1]
                if tag == 'img':
                    Rhea_360buy_Parser.rhea_360_phone_img = attrs[1][1]
                    Rhea_360buy_Parser.rhea_360_phone_hot = attrs[4][1]
            
            #name    
            if tag == rhea_tag_div and rhea_360_phone_name_class in attrs:
                Rhea_360buy_Parser.rhea_tag_name_find = True
            
            # price
            if tag == rhea_tag_div and rhea_360_phone_price_class in attrs:
                Rhea_360buy_Parser.rhea_tag_price_find = True            
            if Rhea_360buy_Parser.rhea_tag_price_find:
                if tag == 'img' and 'src' in attrs[1]:
                    Rhea_360buy_Parser.rhea_360_phone_price = attrs[1][1]     
                

    
    def handle_endtag(self, tag):
        if  Rhea_360buy_Parser.rhea_tag_find:
            #print "Encountered an end tag :", tag
            if rhea_list_phone_tag == tag:
                Rhea_360buy_Parser.rhea_tag_find = False
            
            if Rhea_360buy_Parser.rhea_tag_find:
                if  tag == rhea_tag_div and Rhea_360buy_Parser.rhea_tag_img_find:
                    Rhea_360buy_Parser.rhea_tag_img_find = False
                if  tag == rhea_tag_div and Rhea_360buy_Parser.rhea_tag_name_find:
                    Rhea_360buy_Parser.rhea_tag_name_find = False    
                if  tag == rhea_tag_div and Rhea_360buy_Parser.rhea_tag_price_find:
                    Rhea_360buy_Parser.rhea_tag_price_find = False   
                    Rhea_360buy_Parser.rhea_tag_phone_find = False
                    print "---------------"
                    print Rhea_360buy_Parser.rhea_360_phone_id
                    print Rhea_360buy_Parser.rhea_360_phone_url
                    print Rhea_360buy_Parser.rhea_360_phone_img
                    print Rhea_360buy_Parser.rhea_360_phone_price
                    print Rhea_360buy_Parser.rhea_360_phone_name.decode('gb2312')
                    #print Rhea_360buy_Parser.rhea_360_phone_hot
                    print "*********************"
            
    def handle_data(self, data):
        if  Rhea_360buy_Parser.rhea_tag_find:
            #print "Encountered some data  :", data.decode('gb2312')
            if Rhea_360buy_Parser.rhea_tag_name_find:
                Rhea_360buy_Parser.rhea_360_phone_name = data

                        

class Rhea_360buy_Page_Parser(HTMLParser):
    
    page_tag ='div'
    page_tag_class = ('class','pagin fr')
    page_tag_find = False
    
    page_span_tag ='span'
    page_span_class = ('class', 'text')
    page_span_find = False
    page_need_span = False
    
    page_count = 0;
    page_list = []
    page_url =''
        
    def handle_starttag(self, tag, attrs):
            if self.page_tag == tag and self.page_tag_class in attrs:
                self.page_tag_find = True
                print "find phone page start",tag
            if self.page_tag_find:
                print "phone page tag:", tag, attrs
                if tag == 'a' and 'href' in attrs[0] and self.page_need_span == False:
                    self.page_url = attrs[0][1]
                    ## avoid repeat url link
                    if 'html' in self.page_url and self.page_url not in self.page_list:
                        self.page_list.append(self.page_url)  
                         
                ## process span page                            
                if tag == self.page_span_tag and self.page_span_class in attrs:
                    self.page_span_find = True
                   
                   
                               
    def handle_endtag(self, tag):
        if self.page_tag_find:
            print "Encountered an end tag :", tag
            
            if self.page_span_find and  tag == self.page_span_tag:
                self.page_need_span = True
                
            if self.page_tag == tag:
                self.page_tag_find = False
                self.page_span_find = False
                print "Phone page lists:",self.page_list
                print "Pone page count:",self.page_count
                

    def handle_data(self, data):
        if self.page_tag_find:
            print "Encountered some data  :", data.decode('gb2312')
            if self.page_need_span:
                self.page_count = data.decode('gb2312');
                
                self.page_need_span = False
                



#f = urllib.urlopen("http://www.360buy.com/products/652-653-655-0-1270-0-0-0-0-0-1-1-1.html")
f = urllib.urlopen("http://www.360buy.com/products/652-653-655.html")

#f = urllib.urlopen("http://www.baidu.com")
s = f.read()
##print s
f.close()

parser1 = Rhea_360buy_Page_Parser()
parser1.feed(s)

#for rhea_url in aList:
#    f1 = urllib.urlopen("http://www.360buy.com/products/" + rhea_url)
#    s1 = f1.read()
#    f1.close()
#    parser2 = Rhea_360buy_Parser()
#    parser2.feed(s1)

#parser2 = Rhea_360buy_Parser()
#parser2.feed(s)

#parser2 = Rhea_360buy_Brand_Parser()
#parser2.feed(s)

#print aList

    



